package cn.retalsystem.controller;

import cn.retalsystem.bean.Buding;
import cn.retalsystem.service.BudingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 官宝星
 * @Date: 2019/11/1 15:46
 * @Version 1.0
 * @描述：
 */
@Controller
@RequestMapping("/buding")
public class BudingController {

    @Autowired
    BudingService budingService;
    /**
     * 前端获取所有楼栋信息
     * @return
     */
    @RequestMapping("/getAllBuding")
    @ResponseBody
    public List<Buding> getAllBuding(){

        return budingService.selectAllBuding();
    }

    /**
     * 跳转到楼栋管理页面
     * @param
     * @return
     */
    @RequestMapping("/toBudingView")
    public String toBudingView(){
        return "buding";
    }

    /**
     * 前端获取楼栋信息总数
     * @return
     */
    @RequestMapping("/getCount")
    @ResponseBody
    public int getCount(){
        return budingService.selectCount();
    }

    /**
     * 分页查询楼栋信息
     * @param index 当前页
     * @param size 单页楼栋信息数量
     * @return
     */
    @RequestMapping("/getBudingByPage")
    @ResponseBody
    public List<Buding> getBudingByPage(int index,int size){

        return budingService.selectBudingByPage(index,size);
    }

    /**
     * 上传图片，返回图片路径
     * @param file
     * @return 返回文件路径
     */
    @RequestMapping("/upload")
    @ResponseBody
    public Map<String,String> upload(HttpServletRequest request,MultipartFile file) throws IOException {
        //获取文件名称
        String filename=file.getOriginalFilename();
        //获取uuid
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //生成响应文件名
        filename="/res/img/"+uuid+"_"+filename;
        Map<String,String> map=new HashMap<>();
        map.put("img",filename);
        //生成存储文件名
        String realPath = request.getServletContext().getRealPath(filename);
        file.transferTo(new File(realPath));
        return map;
    }

    /**
     * 添加楼栋信息
     * @param buding
     * @return
     */
    @RequestMapping("/addBuding")
    @ResponseBody
    public boolean addBuding(@RequestBody Buding buding){
        budingService.addBuding(buding);
        return true;
    }

    /**
     * 前端根据楼栋id获取楼栋信息
     * @param id
     * @return
     */
    @RequestMapping("/getBudingById")
    @ResponseBody
    public Buding getBudingById(Integer id){
        return budingService.selectBudingById(id);
    }

    /**
     * 更新楼栋信息
     * @param buding
     * @return
     */
    @RequestMapping("/updateBuding")
    @ResponseBody
    public boolean update(@RequestBody Buding buding){
        budingService.updateBuding(buding);
        return true;
    }

    /**
     * 删除楼栋信息，同时根据图片路径删除图片文件
     * @param buding
     * @return
     */
    @RequestMapping("/delBuding")
    @ResponseBody
    public boolean delBuding(@RequestBody Buding buding,HttpServletRequest request){
        //删除数据库中的楼栋信息
        budingService.delBuding(buding.getId());
        //获取文件对象
        String filepath = buding.getImg();
        String realPath = request.getServletContext().getRealPath(filepath);
        File file=new File(realPath);
        if(file.exists()){
            file.delete();
        }
        return true;
    }
}
